package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.service.dto.*;
import com.example.ymm.modules1.service.user.*;
import com.example.ymm.util.UtilValidate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/clazz")
@Tag(name= "pc端 班级管理")
@Slf4j
@CrossOrigin
public class SysClazzController extends BaseController {

    @Autowired
    DbClazzService dbClazzService;
    @Autowired
    DbClazzLevelService dbClazzLevelService;
    @Autowired
    DbChatGroupUserService dbChatGroupUserService;

    @Autowired
    DbUserService dbUserService;

    @Autowired
    DbClazzBookService dbClazzBookService;

    @Operation(summary = "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(@Valid DbClazzQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbClazz> list = dbClazzService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary = "保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult save(DbClazz clazz,String levels) {
        try{
            dbClazzService.saveOrUpdate(clazz);
            dbClazzLevelService.setLevels(clazz.getClazzId(), levels);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary = "详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public CommonResult detail(@Parameter(name = "数据主键") @RequestParam(required = true) String clazzId) {
        try{
            DbClazz clazz = dbClazzService.getById(clazzId);
            DbClazzLevelQueryCriteria criteria=new DbClazzLevelQueryCriteria();
            criteria.setClazzId(clazzId);
            List<DbClazzLevel> list = dbClazzLevelService.queryAll(criteria);
            clazz.setClazzLevels(list);
            return CommonResult.success(clazz);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@Parameter(name = "数据主键") @RequestParam(required = true) String clazzId) {
        try{
            dbClazzService.removeById(clazzId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary = "班级设置 职级")
    @RequestMapping(value = "/setLevels", method = RequestMethod.POST)
    public CommonResult setLevels(String clazzId,String levels) {
        try{
            dbClazzLevelService.setLevels(clazzId, levels);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary = "班级查看 职级")
    @RequestMapping(value = "/getLevels", method = RequestMethod.POST)
    public CommonResult getLevels(String clazzId) {
        try{
            DbClazzLevelQueryCriteria criteria=new DbClazzLevelQueryCriteria();
            criteria.setClazzId(clazzId);
            List<DbClazzLevel> list = dbClazzLevelService.queryAll(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary = "班级 查看成员")
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST)
    public CommonResult getUsers(DbUserQueryCriteria criteria, YmPageDto ymPage) {
        try{

            if(UtilValidate.isNotEmpty(criteria.getLevelNames())){
                String[] split = criteria.getLevelNames().split(",");
                List<String> list = Arrays.asList(split);
                criteria.setLevelName(list);
            }
            TableList<DbUser> list = dbUserService.list(criteria, ymPage);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary = "班级下拉")
    @RequestMapping(value = "/clazzSelects", method = RequestMethod.POST)
    public CommonResult clazzSelects(DbClazzQueryCriteria criteria) {
        try{

            List<DbClazz> list = dbClazzService.queryAll(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

//    @Operation(summary = "班级 设置成员")
//    @RequestMapping(value = "/setUsers", method = RequestMethod.POST)
//    public CommonResult setUsers(String clazzId,String userIds) {
//        try{
//            String[] split = userIds.split(",");
//            DbClazz clazz = dbClazzService.getById(clazzId);
//            List<String> strings = Arrays.asList(split);
//            List<DbUser> dbUsers = dbUserService.listByIds(strings);
//            for(DbUser user:dbUsers){
//                user.setClazzId(clazzId);
//                user.setClazzName(clazz.getClazzName());
//            }
//            dbUserService.saveOrUpdateBatch(dbUsers);
//            return CommonResult.success();
//        }catch (Exception e){
//            log.error(e.getMessage(),e);
//            return CommonResult.error(e.getMessage());
//        }
//    }


    @Operation(summary = "查看书本与班级的关联信息")
    @RequestMapping(value = "/getBooks", method = RequestMethod.POST)
    public CommonResult getBooks(DbClazzBookQueryCriteria criteria) {
        try{
//            List<DbClazzBook> clazzBook = dbClazzBookService.getClazzBook(criteria);
            List<ViewBook> list = dbClazzBookService.queryAll(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary = "班级 查看书本（多个班级名称逗号隔开）")
    @RequestMapping(value = "/bookClazzNameList", method = RequestMethod.POST)
    public CommonResult bookClazzNameList(DbClazzBookQueryCriteria criteria, YmPageDto ymPage) {
        try{

            TableList<ViewBook> list = dbClazzBookService.bookClazzNameList(criteria, ymPage);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary = "班级 设置书本")
    @RequestMapping(value = "/setBooks", method = RequestMethod.POST)
    public CommonResult setBooks(String clazzId,String bookIds) {
        try{
            dbClazzBookService.setBooks(clazzId,bookIds);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


}
